package model.ability;

import java.util.Random;

import model.core.Logger;
import model.entity.Entity;
import model.stats.StatsContainer;

public class Observe extends InteractiveAbility {
	/**
	 * Constructor for Observe.
	 * @param name
	 * @param ps
	 * @param mana
	 * @param delay
	 */
	public Observe(String name, PassiveSkill ps, int mana, int delay) {
		super(name, ps, mana, delay);
	}

	/**
	 * Observe interaction.  
	 */
	public void interactWithTarget(Entity caster, Entity target) {
		if(this.testProbability())
			this.observeTarget(target);
		else{
			target.makeHostile(caster);
			Logger.getInstance().writeToLogger("Ability failed!");
		}
	}
	
	/**
	 * Observe some stats of the target Entity.
	 * Guaranteed to observe: CurrentHealth, CurrentMana,
	 * OffensiveRating, DefensiveRating and Level
	 * May observe one of: Intellect, Strength, Agility
	 */
	private void observeTarget(Entity target){
		Logger log = Logger.getInstance();
		StringBuilder data = new StringBuilder();
		StatsContainer stats = target.getStatsContainer();
		Random rand = new Random();
		
		int statModifier = (int)(this.getSkillLevelMultiplier() * rand.nextInt(3));
		
		// apply +/- the statModifier to the actual stat
		int health = stats.getCurrentHealth() + (rand.nextInt(2) == 1 ? statModifier :
			(-1) * statModifier);
		int mana = stats.getCurrentMana() + (rand.nextInt(2) == 1 ? statModifier :
			(-1) * statModifier);
		int offensiveRating = stats.getOffenseRating() + (rand.nextInt(2) == 1 ? statModifier :
			(-1) * statModifier);
		int defensiveRating = stats.getDefenseRating() + (rand.nextInt(2) == 1 ? statModifier :
			(-1) * statModifier);
		int level = stats.getLevel() + (rand.nextInt(2) == 1 ? statModifier :
			(-1) * statModifier);
			
		// add one of Strength, Intellect, or Agility to the output (random)
		int extraSkillChoice = rand.nextInt(3);
		int extraSkill = 0;
		
		switch(extraSkillChoice){
			case 0: extraSkill = stats.getStrength() + (rand.nextInt(2) == 1 ? statModifier :
				(-1) * statModifier);
			case 1: extraSkill = stats.getIntellect() + (rand.nextInt(2) == 1 ? statModifier :
				(-1) * statModifier);
			case 2: extraSkill = stats.getAgility() + (rand.nextInt(2) == 1 ? statModifier :
				(-1) * statModifier);
			default: extraSkill = -1;
		}
		
		// add all the stats to the output
		data.append("Entity Stats: ");
		data.append("CurrentHealth = " + Math.max(health, 1) + " | ");
		data.append("CurrentMana = " + Math.max(mana, 1) + " | ");
		data.append("CurrentOffense = " + Math.max(offensiveRating, 1) + " | ");
		data.append("CurrentDefense = " + Math.max(defensiveRating, 1) + " | ");
		data.append("Level = " + Math.max(level, 1) + " | ");
		
		switch(extraSkillChoice){
		case 0: data.append("Strength = " + Math.max(extraSkill, 1) + " | ");
		case 1: data.append("Intellegince = " + Math.max(extraSkill, 1) + " | ");
		case 2: data.append("Agility = " + Math.max(extraSkill, 1) + " | ");
		default: data.append("Error!");
		}
		
		log.writeToLogger(data.toString());
	}

}
